<template>
  <!-- 放置弹层组件 -->
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @open="handleOpen" @close="handleCloseDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width: 80%" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width: 80%" placeholder="1-30个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
          <el-option v-for="item in employeesList" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="form.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="clickSubmit">确定</el-button>
      <el-button size="small" @click="handleCloseDialog">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployees } from '@/api/employees'
import { reqAddDepartment, reqGetDetailDepart, reqUpdateDepartment } from '@/api/departments'
export default {
  name: 'AddDept',
  // 需要传入一个props变量来控制 显示或者隐藏
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    },
    departsList: {
      type: Array,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, value, callback) => {
      let arr = []
      if (this.form.id) {
        // 编辑操作
        if (this.nodeData.name === value) {
          callback()
          return
        }
        arr = this.departsList.filter(item => item.pid === this.nodeData.pid)
      } else {
        // 添加操作
        arr = this.departsList.filter(item => this.nodeData.id === item.pid)
      }
      const flag = arr.some(item => item.name === value)
      flag ? callback(new Error(`同级部门已经有${value}的部门了`)) : callback()
    }
    const checkCodeRepeat = (rule, value, callback) => {
      if (this.form.id && this.nodeData.code === value) {
        callback()
        return
      }
      const isRepeat = this.departsList.some(item => item.code === value)
      isRepeat ? callback(new Error(`同级部门已经有${value}的部门了`)) : callback()
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 30, message: '部门名称必须为1-30个字符', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: '1', max: '30', message: '部门编码必须为1-30个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: '1', max: '300', message: '部门介绍必须为1-300个字符', trigger: ['blur', 'change'] }
        ]
      },
      employeesList: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '添加部门'
    }
  },
  methods: {
    handleOpen() {
      this.getEmployees()
    },
    handleCloseDialog() {
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 管理者
        introduce: '' // 部门介绍
      }
      this.$emit('update:showDialog', false)
      this.$refs.form.resetFields()
    },
    async EditDepartDetail() {
      const { data } = await reqGetDetailDepart(this.nodeData.id)
      console.log(data, 'hhh')
      this.form = data
    },
    async getEmployees() {
      const { data } = await reqGetEmployees()
      this.employeesList = data
    },
    clickSubmit() {
      this.$refs.form.validate(async flag => {
        if (!flag) return
        if (this.form.id) {
          const res = await reqUpdateDepartment(this.form)
          console.log(res, 'ggg')
          this.$message.success('更新部门成功')
        } else {
          await reqAddDepartment({
            ...this.form,
            pid: this.nodeData.id
          })
          this.$message.success('新增部门成功')
        }

        this.handleCloseDialog()
        this.$emit('AddDepet')
      })
    }
  }
}
</script>
